package com.tmc.web.controller.intair;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.intair.EthBookingReq;
import com.tmc.bean.intair.EthFare;
import com.tmc.bean.intair.EthShoppingReq;
import com.tmc.bean.intair.EthTicketingReq;
import com.tmc.entity.intair.IntairOrder;
import com.tmc.service.intair.EthBookingServiceImpl;
import com.tmc.service.intair.EthPricingServiceImpl;
import com.tmc.service.intair.EthRuleServiceImpl;
import com.tmc.service.intair.EthShoppingServiceImpl;
import com.tmc.service.intair.EthTicketingServiceImpl;
import com.tmc.service.intair.IntairOrderServiceImpl;
import com.tmc.util.IntairUtil;
import com.tmc.util.ObjectUtil;
import com.tmc.web.BaseController;

@Controller
@RequestMapping("/flightSearch")
@SuppressWarnings("unchecked")
public class FlightSearchController extends BaseController {
	
	@Autowired
	private EthShoppingServiceImpl shoppingService;
	
	@Autowired
	private EthPricingServiceImpl pricingService;
	
	@Autowired
	private EthRuleServiceImpl ruleService;
	
	@Autowired
	private EthBookingServiceImpl bookingService;
	
	@Autowired
	private EthTicketingServiceImpl ticketingService;
	
	@Autowired
	private IntairOrderServiceImpl orderService;
	
	@RequestMapping(value="/index")
	public ModelAndView flightSearchPage(HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("req", IntairUtil.createNewShoppingReq());
		map.put("context", context);
		return new ModelAndView("views/search/flight-search",map);
	} 
	
	@RequestMapping(value="/index_1")
	public ModelAndView flightSearchPage4Split(HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("req", IntairUtil.createNewShoppingReq());
		map.put("fareKey", "");
		map.put("context", context);
		return new ModelAndView("views/search/flight-search-split",map);
	} 
	
	@RequestMapping(value="/search-combine-web")
	public ModelAndView searchCombine4Web(EthShoppingReq req, String vendorCode, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		CommonResult result = shoppingService.search(context, JSON.toJSONString(req), vendorCode);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("req", req);
		map.put("fares", (List<EthFare>)result.getObj());
		map.put("context", context);
		return new ModelAndView("views/search/flight-search",map);
	}
	
	@RequestMapping(value="/search-combine-json", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String searchCombine4Json(EthShoppingReq req, String vendorCode){
		CommonResult result = shoppingService.search(new ContextInfo(), JSON.toJSONString(req), vendorCode);
		return result.toJson();
	}
	
	@RequestMapping(value="/search-split-web")
	public ModelAndView searchSplit4Web(EthShoppingReq req, @RequestParam("vendorCode") String vendorCode, @RequestParam("fareKey") String fareKey,
			HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		CommonResult result = this.search4Split(context, JSON.toJSONString(req), vendorCode, fareKey);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("req", req);
		map.put("fareKey", fareKey);
		map.put("fares", (List<EthFare>)result.getObj());
		map.put("context", context);
		return new ModelAndView("views/search/flight-search-split",map);
	}
	
	@RequestMapping(value="/search-split-json", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String searchSplit4Json(EthShoppingReq req, @RequestParam("vendorCode") String vendorCode, @RequestParam("fareKey") String fareKey){
		CommonResult result = this.search4Split(new ContextInfo(), JSON.toJSONString(req), vendorCode, fareKey);
		return result.toJson();
	}
	
	@RequestMapping(value="/pricing-json")
	@ResponseBody
	public String pricing4Json(String fareKey){
		CommonResult result = pricingService.pricing(new ContextInfo(), fareKey);
		return result.toJson();
	}
	
	@RequestMapping(value="/rule-json")
	@ResponseBody
	public String rule4Json(String fareKey){
		CommonResult result = ruleService.rule(new ContextInfo(), fareKey);
		return result.toJson();
	}
	
	@RequestMapping(value="/booking-json")
	@ResponseBody
	public String booking4Json(String req){
		EthBookingReq bookingReq = JSON.parseObject(req, EthBookingReq.class);
		String token = bookingReq.getToken();
		CommonResult result = bookingService.booking(this.getContextInfo(token), bookingReq);
		return result.toJson();
	}
	
	@RequestMapping(value="/ticketing-json")
	@ResponseBody
	public String ticketing4Json(String req){
		EthTicketingReq ticketingReq = JSON.parseObject(req, EthTicketingReq.class);
		CommonResult result = ticketingService.ticketing(new ContextInfo(), ticketingReq);
		return result.toJson();
	}
	
	@RequestMapping(value = "/current")
	@ResponseBody
	public String current(String token) {
		ContextInfo context = this.getContextInfo(token);
		IntairOrder order = orderService.getCurrent(context);
		return JSON.toJSONString(order);
	}
	
	public CommonResult search4Split(ContextInfo context, String reqJSON, String vendorCode, String fareKey) {
		CommonResult result = shoppingService.search(context, reqJSON, vendorCode);
		if (!result.isSuccess()){
			return result;
		}
		List<EthFare> fares = (List<EthFare>)result.getObj();
		if (ObjectUtil.noText(fareKey)){
			return shoppingService.getOutBound(fares);
		}
		return shoppingService.getInBound(fares, fareKey);
	}
}
